Ext.extend(app.module,{
    currentFormValues: {},
    init: function(){
        mainPanel = this;
        //用于列表
        this.grlwlzDs = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'fruit/listForSh.do'}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'},
                {name:'dept'},
                {name:'count'},
                {name:'rank'},
                {name:'state'},
                {name:'userid'},
                {name:'username'},
                {name:'time'},
                {name:'journal'},
                {name:'note'},
                {name:'fruittype'},
                {name:'fruitTypeName'},
                {name:'deptname'}
            ])
        });

        var grlwlz_pagesize_combo = new Ext.form.ComboBox( {       //设置分页栏的每页记录数选择
            name : 'pagesize',
            hiddenName : 'pagesize',
            typeAhead : true,
            triggerAction : 'all',
            lazyRender : true,
            mode : 'local',
            store : new Ext.data.ArrayStore(
                {
                    fields : [ 'value', 'text' ],
                    data : [ [ 20, '10条/页' ], [ 50, '20条/页' ],
                        [ 100, '50条/页' ], [ 200, '100条/页' ]]
                }
            ),
            valueField : 'value',
            displayField : 'text',
            value : '20',
            editable : false,
            width : 85
        });
        var number = parseInt(grlwlz_pagesize_combo.getValue());

        grlwlz_pagesize_combo.on("select", function(comboBox) {
            var bbar = Ext.getCmp('grlwlzbbar');
            bbar.pageSize = parseInt(comboBox.getValue());
            number = parseInt(comboBox.getValue());
            mainPanel.ds.reload( {
                params : {
                    start : 0,
                    limit : bbar.pageSize
                }
            });
        });

        this.body = new Ext.grid.GridPanel({
            store: this.grlwlzDs,
            region: 'center',
            loadMask: true,
            viewConfig:{
                forceFit:true
            },
            columns: [
                new Ext.grid.RowNumberer(),
                {
                    xtype: 'gridcolumn',
                    dataIndex: 'id',
                    header: 'id',
                    hidden: true
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'deptname',
                    header: '所属部门'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'username',
                    header: '姓名'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'name',
                    header: '成果名称'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'dept',
                    header: '成果来源'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'time',
                    header: '成果取得时间'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'journal',
                    header: '论文刊期'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'fruitTypeName',
                    header: '成果种类等级'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'rank',
                    header: '排名'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'count',
                    header: '参与人数'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'state',
                    header: '状态',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        if(value==1) return '已审核';
                        else return '待审核';
                    }
                },
                {
                    xtype: 'actioncolumn',
                    width: 50,
                    header: '审核',
                    align: 'center',
                    items: [
                        {
                            icon: '../images/ext/accept.png',
                            tooltip: '通过',
                            handler: function(grid, rowIndex, colIndex) {
                                var record = grid.getStore().getAt(rowIndex);
                                Ext.Msg.confirm('通过', '确定成果<font color="red">' + record.data.name + '</font>审核通过',
                                    function(btn) {
                                        if (btn == 'yes') {
                                            mainPanel.submitFruitSh(record.data.id,1);
                                        }
                                    }
                                );
                            }
                        },
                        {
                            icon: '../images/ext/delete.png',
                            tooltip: '不通过',
                            handler: function(grid, rowIndex, colIndex) {
                                var record = grid.getStore().getAt(rowIndex);
                                Ext.Msg.confirm('不通过', '确定成果<font color="red">' + record.data.name + '</font>审核不通过',
                                    function(btn) {
                                        if (btn == 'yes') {
                                            mainPanel.submitFruitSh(record.data.id,0);
                                        }
                                    }
                                );
                            }
                        }
                    ]
                }
            ],
            bbar: {
                id: 'grlwlzbbar',
                xtype: 'paging',
                pageSize : number,
                store : this.grlwlzDs,
                displayInfo : true,
                displayMsg : '显示{0}条到{1}条,共{2}条',
                emptyMsg : "没有符合条件的记录",
                items : [ '-', '&nbsp;&nbsp;', grlwlz_pagesize_combo ]
            }
        });
        this.main.add(this.body);
        this.main.doLayout();
        this.grlwlzDs.load({
            params: {
                start:0,
                limit:20
            }
        });
    },
    getgrlwlzDialog: function(){
        mainPanel = this;
        //选择成果类型的下拉菜单所需数据
        var fruitStore = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'fruittype/list.do'}),
            reader: new Ext.data.JsonReader({
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'}
            ])
        });
        var comboFruitType = new Ext.form.ComboBox({
            store: fruitStore,
            editable: false,
            emptyText: '请选择成果类型...',
            mode: 'remote',
            hiddenName : 'fruittype',
            typeAhead : true,
            triggerAction : 'all',
            displayField: 'name',
            valueField: 'id',
            fieldLabel: '成果类型'
        });

        var form = new Ext.form.FormPanel({
            defaultType: 'textfield',
            bodyStyle: 'padding:5px',
            buttonAlign: 'center',
            url:'fruit/save.do',
            labelWidth: 80,
            defaults: {
                anchor: '90%'
            },
            items: [
                {
                    xtype: 'textfield',
                    name: 'name',
                    allowBlank: false,
                    emptyText: '成果名不能为空',
                    fieldLabel: '成果名称'
                },
                comboFruitType,
                {
                    xtype: 'textfield',
                    name: 'dept',
                    fieldLabel: '出版社'
                },
                {
                    xtype: 'textfield',
                    name: 'count',
                    emptyText: '请填写阿拉伯数字，独撰填1或不填',
                    fieldLabel: '参与人数'
                },
                {
                    xtype: 'textfield',
                    name: 'rank',
                    emptyText: '请填写阿拉伯数字,独撰填0或不填',
                    fieldLabel: '排名'
                }
            ],
            buttons: [{
                text: '确定',
                handler: function() {
                    if (form.getForm().isValid()) {
                        form.getForm().submit({
                            waitTitle: '保存数据',
                            waitMsg: '正在保存……',
                            scope: this,
                            method: 'post',
                            params: '',
                            submitEmptyText: false,     //禁止提交emptyText
                            success: function(form, action) {
                                var info = action.result.info;
                                if (action.result.success) {
                                    mainPanel.grlwlzDs.reload();
                                    dlg.close();
                                }
                                Ext.Msg.alert('信息', info);
                            },
                            failure: function(form, action) {
                                var info = action.result.info;
                                Ext.Msg.alert('提示', info);
                            }
                        });
                    }
                }
            },
                {
                    text: '重置',
                    handler: function() {
                        var f = form.getForm();
                        f.reset();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
                    }
                }]
        });

        var dlg = new Ext.Window({
            height: 230,
            width: 400,
            layout: 'fit',
            border: false,
            frame: true,
            title: '',
            modal: true,
            plain: true,
            resizable: false,
            items: [form],
            listeners: {
                scope: this,
                render: function(fp){
                    form.getForm().waitMsgTarget = fp.getEl();
                },
                show: function(){
                    var f = form.getForm();
                    f.setValues(this.currentFormValues);
                    f.clearInvalid();
                }
            }
        });
        dlg.show();
    },
    submitFruitSh: function(id,isagree){
        mainPanel = this;
        Ext.Ajax.request({
            method:'post',
            url:'fruit/submitFruitSh.do',
            params:{
                isagree:isagree,
                id:id
            },
            success: function(resp, opts) {
                var result = Ext.util.JSON.decode(resp.responseText);
                var info = result.info;
                if(result.success=='true') {
                    mainPanel.grlwlzDs.reload();
                    Ext.Msg.alert('信息', info);
                }else {
                    Ext.Msg.alert('信息', info);
                }
            },
            failure: function(resp,opts) {
                var result = Ext.util.JSON.decode(resp.responseText);
                var info = result.info;
                Ext.Msg.alert('提示', info);
            }
        });
    }
});

